package com.lfz.manage.rabbit;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lfz.common.exception.MsgException;
import com.lfz.common.vo.SysResult;
import com.lfz.manage.pojo.Company;
import com.lfz.manage.service.CompanyService;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

import java.io.IOException;

import static com.lfz.manage.config.Constant.MAPPER;

@Component
public class AsyncTask {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CompanyService companyService;


    @Async
    public ListenableFuture<String> PutCompanyOperation(String message) {

        SysResult result = null;
        try {
            Company company = MAPPER.readValue(message, Company.class);
            companyService.addCompany(company);
            result = SysResult.OK;
        } catch (IOException | MsgException e) {
            e.printStackTrace();
            result = SysResult.OPERATE_FAILED;
            result.setData(ExceptionUtils.getStackTrace(e));
        }
        String json = null;
        try {
            json = MAPPER.writeValueAsString(result);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(json);
    }
}